package com.example.controller;

import com.alibaba.excel.util.StringUtils;
import com.example.dto.StuDto;
import com.example.dto.StuFindDto;
import com.example.dto.StuInsertDormitoryByZhiYe;
import com.example.exception.GlobalExceptionHandler;
import com.example.pojo.Category;
import com.example.pojo.Dormitory;
import com.example.pojo.Result;
import com.example.pojo.User;
import com.example.service.CategoryService;
import com.example.service.DormitoryService;
import com.example.service.UserService;
import com.example.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Size;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/stu")
@Slf4j
public class StuController {

    @Autowired
    private UserService userService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DormitoryService dormitoryService;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

//    @PostMapping("/login")
//    public Result loginStu(@RequestBody StuDto stuDto){
//
//        Integer id = stuDto.getId();
//        String name = stuDto.getName();
//
//        User emp = userService.findUser(name);
//        if(emp==null){
//            return Result.error("没有这个学生");
//        }else{
//            if(emp.getId()==id){
//                Map<String, Object> claims = new HashMap<>();
//                claims.put("id", emp.getId());
////                claims.put("name", emp.getName());
//                String token = JwtUtil.genToken(claims);
//
//                return Result.success(token);
//            }else{
//                return Result.error("密码错误");
//            }
//        }
//    }

    @PostMapping("/login")
    public Result loginStu(@RequestBody StuDto stuDto) throws Exception {

        Integer id = stuDto.getId();
        String name = stuDto.getName();
        String phone = stuDto.getPhone();
        String code = stuDto.getCashCode();

        //TODO  校验参数
        //校验参数
//        if(id==null ||
//                StringUtils.isEmpty(name) ||
//                StringUtils.isEmpty(phone) ||
//                StringUtils.isEmpty(code)) {
//            throw new Exception("参数不能为空");
//        }

        //TODO 测试
//        String codeValueRedis = redisTemplate.opsForValue().get("phone:code" + phone);
//        if(!code.equals(codeValueRedis)){
//            //验证码不同
//            try {
//                throw new Exception("验证码错误");
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }

        User emp = userService.findUser(name);
        if(emp==null){
            return Result.error("没有这个学生");
        }else{
            if(emp.getId()==id){
                Map<String, Object> claims = new HashMap<>();
                claims.put("id", emp.getId());
//                claims.put("name", emp.getName());
                String token = JwtUtil.genToken(claims);
                return Result.success(token);
            }else{
                return Result.error("密码错误");
            }
        }
    }

    @GetMapping("/findCategory")
    public Result listStu(){
        List<Category> list = categoryService.findAll();
//        for (Category category : list) {
//            System.out.println(category);
//        }
        return Result.success(list);
    }

    @GetMapping("/findDormitory")
    public Result listStuDormitory(){
        List<Dormitory> list = dormitoryService.findAll();
        return Result.success(list);
    }


    //置业大厦查询
    @GetMapping("/findByZhiYe")
    public Result listByZhiYe(){
        List<Dormitory> list = dormitoryService.findByZhiYe();
        return Result.success(list);
    }

    //双创四人间查询
    @GetMapping("/findByShuangChuangSiRen")
    public Result listByShuangChuangSiRen(){
        List<Dormitory> list = dormitoryService.findByShuangChuangSiRen();
        return Result.success(list);
    }

    //双创双人间查询
    @GetMapping("/findByShuangChuangShuangRen")
    public Result listByShuangChuangShuangRen(){
        List<Dormitory> list = dormitoryService.findByShuangChuangShuangRen();
        return Result.success(list);
    }

    //71四人间间查询
    @GetMapping("/findByQiYiSiRen")
    public Result listByQiYiSiRen(){
        List<Dormitory> list = dormitoryService.findByQiYiSiRen();
        return Result.success(list);
    }

    //101四人间间查询
    @GetMapping("/findByYaoLinYaoSiRen")
    public Result listByYaoLinYaoSiRen(){
        List<Dormitory> list = dormitoryService.findByYaoLinYaoSiRen();
        return Result.success(list);
    }

    //19四人间间查询
    @GetMapping("/findByShiJiuSiRen")
    public Result listByShiJiuSiRen(){
        List<Dormitory> list = dormitoryService.findByShiJiuSiRen();
        return Result.success(list);
    }

    //更新宿舍号,宿舍名，选择成功
    @PutMapping("/insert")
    public Result insertDormitory(@RequestBody StuInsertDormitoryByZhiYe stuInsertDormitoryByZhiYe){
        userService.addDormitory(stuInsertDormitoryByZhiYe);
        return Result.success("选择宿舍成功");
    }

    //获取个人信息
    @GetMapping("/findByMe")
    public Result getStuInfo(@RequestParam Integer id){
        User user = userService.getUserInfo(id);
        return Result.success(user);
    }

    /**
     * 根据分类名和宿舍号查询有哪些学生
     */

    @GetMapping("/findStu")
    public Result findStuINfo(@RequestBody StuFindDto stuFindDto){
        User user = userService.findStu(stuFindDto);
        log.info("查询此宿舍有哪些学生");
        return Result.success(user);
    }
}
